package com.example.analysis.service.impl;

import com.example.analysis.dto.ClusterScoreDto;
import com.example.analysis.entity.Course;
import com.example.analysis.entity.Student;
import com.example.analysis.entity.StudentAbility;
import com.example.analysis.entity.StudentWarning;
import com.example.analysis.mapper.StudentDao;
import com.example.analysis.service.StudentService;
import com.example.analysis.utils.Response;
import com.example.analysis.vo.AbilityDimensionVo;
import com.example.analysis.vo.CourseVo;
import com.example.analysis.vo.SemesterWarningVo;
import com.example.analysis.vo.StudentCourseVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class StudentServiceImpl implements StudentService {
    @Resource
    private StudentDao studentDao;

    @Override
    public Response<Student> insert(Student aStudent) {
        try {
            studentDao.insert(aStudent);
            return new Response<>(true, "Insert success", null);
        } catch (Exception e) {
            return new Response<>(false, "Insert failed: " + e.getMessage(), null);
        }
    }

    @Override
    public Response<Student> getById(String StudentId) {
        Student aStudent = studentDao.selectByPrimaryKey(StudentId);
        if (aStudent != null) {
            return new Response<>(true, "Success", aStudent);
        } else {
            return new Response<>(false, "Administration not found", null);
        }
    }

    @Override
    public List<ClusterScoreDto> getScoreSet(String className, String subName) {
        return studentDao.getScoreSet(className, subName);
    }

//    @Override
//    public Response<Student> getByName(String StudentName) {
//
//    }
@Override
public Response<StudentCourseVo> getCourseTable(String studentId) {
    Student student = studentDao.getStudentById(studentId);
    if (student != null) {
        StudentCourseVo vo = new StudentCourseVo();
        vo.setStudentId(student.getStudentId());
        vo.setStudentName(student.getName());

        // 转换List<Course>为List<CourseVo>
        List<CourseVo> courseVos = new ArrayList<>();
        for (Course course : student.getCourses()) {
            CourseVo courseVo = new CourseVo();
            courseVo.setSubId(course.getSubId());
            courseVo.setCourseName(course.getCourseName());
            // 其他属性转换
            courseVos.add(courseVo);
        }
        vo.setCourses(courseVos);

        return new Response<>(true, "Success", vo);
    } else {
        return new Response<>(false, "Student not found", null);
    }
}

    @Override
    public Response<AbilityDimensionVo> getAbilityDimension(String studentId) {
        Student student = studentDao.getStudentById(studentId);
        if (student != null) {
            AbilityDimensionVo vo = new AbilityDimensionVo();
            vo.setProfessionalAbility(student.getAbilityDimension().getProfessionalAbility());
            vo.setGeneralAbility(student.getAbilityDimension().getGeneralAbility());
            vo.setPracticalAbility(student.getAbilityDimension().getPracticalAbility());
            vo.setInnovativeAbility(student.getAbilityDimension().getInnovativeAbility());
            vo.setQualityAbility(student.getAbilityDimension().getQualityAbility());
            return new Response<>(true, "Success", vo);
        } else {
            return new Response<>(false, "Student not found", null);
        }
    }

    @Override
    public Response<SemesterWarningVo> getSemesterWarning(String studentId) {
        Student student = studentDao.getStudentById(studentId);
        if (student != null) {
            SemesterWarningVo vo = new SemesterWarningVo();
            vo.setStudentId(student.getStudentId());
            vo.setHasWarning(student.isSemesterWarning());
            vo.setWarningMessage(student.isSemesterWarning() ? "您有学期预警，请及时处理！" : "无预警信息");
            return new Response<>(true, "Success", vo);
        } else {
            return new Response<>(false, "Student not found", null);
        }
    }
    @Override
    public List<StudentAbility> getClassStudentAbilities(String classId) {
        // 这里模拟一些数据
        List<StudentAbility> abilities = new ArrayList<>();
        abilities.add(new StudentAbility("001", "张三", Map.of("数学", 85.0, "语文", 78.0), new Date()));
        abilities.add(new StudentAbility("002", "李四", Map.of("数学", 90.0, "语文", 88.0), new Date()));
        return abilities;
    }

    @Override
    public List<StudentWarning> getStudentsWithWarnings(String classId) {
        // 这里模拟一些数据
        List<StudentWarning> warnings = new ArrayList<>();
        warnings.add(new StudentWarning("001", "张三", "数学成绩低于70分"));
        warnings.add(new StudentWarning("002", "李四", "语文成绩低于60分"));
        return warnings;
    }

    @Override
    public void saveGuidance(String studentId, String guidance) {
        System.out.println("学生ID: " + studentId + "，指导意见: " + guidance);
    }

}
